package Pages;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;

import java.util.Map;

import static Driver.DriverSingleton.getChromeDriver;
import static Driver.DriverSingleton.getVars;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;

//登录
public class LoginPage {
    WebDriver driver = getChromeDriver();
    Map<String,Object> vars = getVars();

    //进入网页
    public LoginPage goLoginPage(){
        driver.get("https://sei-test.021hqit.com/group-6");
        assertThat(driver.getTitle(), is("登录"));
        return this;
    }

    //
    public LoginPage clickInput1(){
        driver.findElement(By.cssSelector(".el-form-item:nth-child(1) .el-input__inner")).click();
        return this;
    }

    //
    public LoginPage clickInput2(){
        driver.findElement(By.cssSelector(".el-form-item:nth-child(1) .el-input__inner")).click();
        return this;
    }

    //输入用户名
    public LoginPage enterUsername(String keys){
        driver.findElement(By.cssSelector(".el-form-item:nth-child(1) .el-input__inner")).sendKeys(keys);
        return this;
    }

    //输入密码
    public LoginPage enterPassword(String keys){
        driver.findElement(By.cssSelector(".el-form-item:nth-child(2) .el-input__inner")).sendKeys(keys);
        return this;
    }

    //按下登录键
    public LoginPage clickButton(){
        driver.findElement(By.cssSelector(".el-button")).click();
        return this;
    }

    //弹窗消息
    public String getMessage(){
        waitForWindow(2000);
        WebElement elementText = driver.findElement(By.cssSelector("div.el-message"));
        String info = elementText.getText();
        return info;
    }

    //错误提示
    public String getErrorMessage(){
        waitForWindow(2000);
        WebElement elementText = driver.findElement(By.cssSelector("div.el-form-item__error"));
        String info = elementText.getText();
        return info;
    }

    public String getErrorMessage1(){
        waitForWindow(2000);
        WebElement elementText = driver.findElement(By.cssSelector(".el-form-item:nth-child(1) .el-form-item__error"));
        String info = elementText.getText();
        return info;
    }

    public String getErrorMessage2(){
        waitForWindow(2000);
        WebElement elementText = driver.findElement(By.cssSelector(".el-form-item:nth-child(2) .el-form-item__error"));
        String info = elementText.getText();
        return info;
    }

    public LoginPage goHomePage(){
        //进入到首页
        assertThat(driver.getTitle(), is("首页"));
        return this;
    }

    //成功登录的方法
    public LoginPage success(){
        goLoginPage().
                clickInput1().
                enterUsername("ecnu_admin").
                clickInput2().
                enterPassword("123456").
                clickButton().waitForWindow(2000);
        return this;
    }

    public LoginPage waitForWindow(int timeout){
        try {
            Thread.sleep(timeout);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return this;
    }

}
